﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Tools.LaserPoint
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Storyboard _moveLaserPointerStoryboard = null;
        DoubleAnimation _moveLaserPointerAnimationTop = null;
        DoubleAnimation _moveLaserPointerAnimationLeft = null;
        private double duration = 0.3d;

        private double? _curTop = null;
        private double? _curLeft = null;

        public MainWindow()
        {
            InitializeComponent();

            //Storyboard sbFlash = this.Resources["_flash"] as Storyboard;
            //if(sbFlash!=null)
            //{
            //    sbFlash.Begin(this);
            //}
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            if(e.RightButton == MouseButtonState.Pressed)
            {
                ShowLP(e.GetPosition(this).X, e.GetPosition(this).Y);
            }
        }


        protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
        {
            Point p =  e.GetPosition(this);
            System.Diagnostics.Debug.WriteLine(string.Format("{0},{1}", p.X, p.Y));
            if(p.X < 40 && p.Y < 40)
            {
                this.HideLP();
            }
            base.OnPreviewMouseDoubleClick(e);
        }

        public void ShowLP(double toX, double toY)
        {
            this.Dispatcher.Invoke(() =>
            {

                if (this._ctlImageLaserPoint.Visibility != System.Windows.Visibility.Visible)
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                    this._ctlImageLaserPoint.Visibility = System.Windows.Visibility.Visible;
                    this.Topmost = true;
                }

                double halfW = 90 / 2;
                double halfH = 90 / 2;
                if (_curTop == null
                    && _curLeft == null)
                {
                    this._curTop = toY - halfH;
                    this._curLeft = toX - halfW;
                    Canvas.SetTop(this._ctlImageLaserPoint, _curTop.Value);
                    Canvas.SetLeft(this._ctlImageLaserPoint, _curLeft.Value);
                }
                else
                {
                    if (_moveLaserPointerStoryboard == null)
                    {
                        _moveLaserPointerStoryboard = new Storyboard();

                        _moveLaserPointerAnimationTop = new DoubleAnimation(this._curTop.Value, toY - halfH, new Duration(TimeSpan.FromSeconds(duration)));
                        Storyboard.SetTarget(_moveLaserPointerAnimationTop, this._ctlImageLaserPoint);
                        Storyboard.SetTargetProperty(_moveLaserPointerAnimationTop, new PropertyPath(Canvas.TopProperty));

                        _moveLaserPointerAnimationLeft = new DoubleAnimation(this._curLeft.Value, toX - halfW, new Duration(TimeSpan.FromSeconds(duration)));
                        Storyboard.SetTarget(_moveLaserPointerAnimationLeft, this._ctlImageLaserPoint);
                        Storyboard.SetTargetProperty(_moveLaserPointerAnimationLeft, new PropertyPath(Canvas.LeftProperty));

                        _moveLaserPointerStoryboard.Children.Add(_moveLaserPointerAnimationTop);
                        _moveLaserPointerStoryboard.Children.Add(_moveLaserPointerAnimationLeft);
                        _moveLaserPointerStoryboard.Begin(this);
                    }
                    else
                    {
                        double left = Canvas.GetLeft(this._ctlImageLaserPoint);
                        double top = Canvas.GetTop(this._ctlImageLaserPoint);
                        _moveLaserPointerAnimationTop.From = top;
                        _moveLaserPointerAnimationTop.To = toY - halfH;

                        _moveLaserPointerAnimationLeft.From = left;
                        _moveLaserPointerAnimationLeft.To = toX - halfW;
                        _moveLaserPointerStoryboard.Begin(this);
                    }
                }
            });
        }

        public void HideLP()
        {
            this.Dispatcher.Invoke(() =>
            {
                this.Visibility = System.Windows.Visibility.Hidden;
                this._ctlImageLaserPoint.Visibility = System.Windows.Visibility.Hidden;
                this.Topmost = false;
            });
        }

        public void CloseLP()
        {
            this.Dispatcher.Invoke(() =>
            {
                try
                {
                    this.Close();
                }
                catch (Exception ex)
                {

                }
            });
        }
    }
}
